﻿using System;
using Microsoft.Xna.Framework;
using CellarDoor.Gui.Elements;

namespace CellarDoor.Gui.Animation
{
    public class AnimatePosition : AnimateComponent
    {
        /// <summary>
        /// The start position
        /// </summary>
        public Vector2 StartPosition { get; set; }

        /// <summary>
        /// The end position
        /// </summary>
        public Vector2 EndPosition { get; set; }

        public bool SetX { get; set; }
        public bool SetY { get; set; }

        public AnimatePosition(UiElement element)
            : base(element)
        {
            SetX = true;
            SetY = true;
        }

        public AnimatePosition(UiElement element, Vector2 start, Vector2 end, float durationInMs)
            : this(element)
        {
            SetX = true;
            SetY = true;
            StartPosition = start;
            EndPosition = end;
            Duration = durationInMs;
        }

        protected override void Animate()
        {
            Vector2 offset = StartPosition + (EndPosition - StartPosition) * EasedProgress;
            if (!SetX)
                offset.X = Target.Location.X;
            if (!SetY)
                offset.Y = Target.Location.Y;
            Target.Location = offset;
        }
    }
}
